//
//  LcSingleton.m
//  18Birdies
//
//  Created by Lc on 15/3/20.
//  Copyright (c) 2015年 Lc. All rights reserved.
//

#import "LcSingleton.h"

@interface LcSingleton () {
  BOOL _internalInited;
}

@end

@implementation LcSingleton

static NSMutableDictionary* instances;

+ (void)initialize {
  if (self == [LcSingleton self]) {
    instances = [NSMutableDictionary dictionary];
  }
}

+ (instancetype)allocWithZone:(struct _NSZone*)zone {
  NSString* clazzName = NSStringFromClass(self);
  @synchronized([LcSingleton self]) {
    id instance = instances[clazzName];
    if (instance == nil) {
      instance = [super allocWithZone:zone];
      instances[clazzName] = instance;
    }
    return instance;
  }
}

+ (instancetype)sharedInstance {
  NSString* clazzName = NSStringFromClass(self);
  @synchronized([LcSingleton self]) {
    LcSingleton* instance = instances[clazzName];
    if (instance == nil) {
      instance = [[self alloc] init];
      instances[clazzName] = instance;
    }
    if (!instance->_internalInited) {
      instance->_internalInited = YES;
      [instance internalInit];
    }
    return instance;
  }
}

- (void)internalInit {
}

@end
